package com.dhecp.project.storage.controller;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.framework.web.service.BaseInfoSimpleService;
import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.storage.domain.TsCargomaintenance;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.domain.SysDictionaryForPageField;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.storage.service.TsCargomaintenanceService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 货物报关信息维护(TsCargomaintenance)控制层
 *
 * @author fxz
 * @since 2024-01-11 15:35:18
 */
@Controller
@RequestMapping("/storage/tsCargomaintenance")
public class TsCargomaintenanceController extends BaseModuleController{


    private String prefix = "storage/tsCargomaintenance";
    private String moduleTable = ModuleTable.TS_CARGOMAINTENANCE;

    @Autowired
    private TsCargomaintenanceService tsCargomaintenanceService;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private BaseInfoSimpleService baseInfoSimpleService;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;


    /**
     * @Description: 打开查询列表页
     * @Param: [params]
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: hlf
     * @date: 2022-05-09 11:39
     */
    @RequiresPermissions("storage:tsCargomaintenance:view")
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        params.put("entity", new TsCargomaintenance()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("prefix", prefix + "/tsCargoMaintenance"); //页面路径
        params.put("toolBarWidth", 80);
        params.put("checkBox", "yes");

        ModelAndView mv = this.openListPage(params);
        mv.addObject("storage", "tsCargoMaintenance");
        return mv;
    }


    /**
     * @Description: 查询页 列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: fxz
     * @date: 2023-09-08 15:43
     */
    @RequiresPermissions("storage:tsCargomaintenance:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        List<TsCargomaintenance> list = new ArrayList<TsCargomaintenance>();
        try {
            list = tsCargomaintenanceService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

    /**
     * 打开渠道代码制单页
     * @Author: fxz
     * @date: 2023-09-13 19:00
     */
    @RequiresPermissions("storage:tsCargomaintenance:edit")
    @GetMapping("/edit")
    public ModelAndView add(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable);
        params.put("entity", new TsCargomaintenance()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        List<SysDictionaryForPageField> dicFieldsList = new ArrayList<SysDictionaryForPageField>(); //字段字典
        List<Object> fieldsList = new ArrayList<Object>(); 			//生成控件的字段和属性
        List<Object> fieldsHiddenList = new ArrayList<Object>(); 	//隐藏的字段和属性
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            //查询数据字典
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }

            params.put("getFieldAttributesType", "pagenotingrid"); 	//非表格字段
            dicFieldsList = dictionaryForFieldService.queryFieldAttributesList(params);
            TsCargomaintenance tsChannelcode;
            if (params.get("guid") == null ){
                tsChannelcode = new TsCargomaintenance();
            }else {
                tsChannelcode = tsCargomaintenanceService.queryFirstRecord(params);
            }
            if (dicFieldsList.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                for (SysDictionaryForPageField dicFields : dicFieldsList){
                    Map<String, Object> fieldsListMap = TypeUtils.entityToMap(dicFields);
                    fieldsListMap.put("fieldContent", TypeUtils.entityToMap(tsChannelcode).get(fieldsListMap.get("fields")));

                    if (fieldsListMap.get("fieldFrontType") == null){
                        fieldsListMap.put("fieldFrontType", "t_text");
                    }

                    //如果有下拉控件，则根据配置获取对应下拉待选内容
                    List<BaseInfo> dropdownList = new ArrayList<BaseInfo>();
                    String fieldFrontType = dicFields.getFieldFrontType();
                    String fieldSearchType = dicFields.getFieldSearchType();
                    String fieldFrontSave = dicFields.getFieldFrontSave();
                    String fieldFrontShow = dicFields.getFieldFrontShow();
                    if (fieldFrontType != null && fieldFrontType.toLowerCase().startsWith("t_dropdown")){
                        if (fieldSearchType != null && !"".equals(fieldSearchType)){
                            if (fieldFrontSave == null || "".equals(fieldFrontSave)){
                                fieldFrontSave = "vid";
                            }
                            if (fieldFrontShow == null || "".equals(fieldFrontShow)){
                                fieldFrontShow = "vname";
                            }
                            dropdownList = baseInfoSimpleService.getDropdownData(fieldSearchType,fieldFrontSave,fieldFrontShow);
                        }
                    }
                    fieldsListMap.put("dropdownList", dropdownList);
                    if ((Integer)fieldsListMap.get("fieldFrontTop") == 1){	//显示的字段
                        fieldsList.add(TypeUtils.mapToJson(fieldsListMap));
                    }else {	//隐藏的字段
                        fieldsHiddenList.add(TypeUtils.mapToJson(fieldsListMap));
                    }
                }
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        Map<String, Object> stringObjectMap = gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
        mv.addObject("gridColumns", stringObjectMap);
        mv.addObject("enableEdit", 1); //当前页面是否可编辑（0：不可编辑，1：可编辑）
        mv.addObject("pageType","add"); //页面类型：新增（add）、修改（edit）
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap)); //各字段及属性
        mv.addObject("fieldsList", fieldsList);				//显示的字段
        mv.addObject("fieldsHiddenList", fieldsHiddenList);	//隐藏的字段
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName(prefix + "/tsCargoMaintenance_edit");
        return mv;
    }


    /**
     * 修改渠道代码
     * @author fxz
     * @date: 2023-09-22
     */
    @RequiresPermissions("storage:tsCargomaintenance:edit")
    @Log(title = "编辑货物报关信息维护", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editpost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= tsCargomaintenanceService.saveData(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }


    /**
     * @Description: 数据导出Excel
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-15 15:43
     */
    @RequiresPermissions("storage:tsCargomaintenance:export")
    @Log(title = "货物报关信息导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable);
        params.put("entity", new TsCargomaintenance());

        List<TsCargomaintenance> list = tsCargomaintenanceService.queryList(params);

        params.put("getFieldAttributesType", "export");
        List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<TsCargomaintenance> util = new ExportExcelUtils<TsCargomaintenance>(moduleTable, new TsCargomaintenance());
        return util.exportExcel(list, fieldList, "货物报关信息维护数据", "export");
    }



    /**
     * 删除货物报关信息
     * @author fxz
     * @date: 2023-09-22
     */
    @RequiresPermissions("storage:tsCargomaintenance:edit")
    @Log(title = "删除货物报关信息", businessType = BusinessType.DELETE)
    @PostMapping("/deletepost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult deletepost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= tsCargomaintenanceService.delete(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

        @GetMapping("/select")
    public ModelAndView select(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new TsCargomaintenance()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("toolBarWidth", 0);
        if(params.get("pagetype") !=null && params.get("pagetype").toString().equalsIgnoreCase("checkbox"))params.put("checkBox", "yes");
        else params.put("radio", "yes");
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();

        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("prefix", prefix);
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName("common/select");
        return mv;
    }

    /**
     * @Description: 查询页 列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: fxz
     * @date: 2023-09-08 15:43
     */
    @RequiresPermissions("storage:tsCargomaintenance:list")
    @GetMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(@RequestParam Map<String, Object> params) {
        params.put("bdeleted",true);
        params.put("moduleTable", moduleTable);
        List<TsCargomaintenance> list = new ArrayList<TsCargomaintenance>();
        try {
            list = tsCargomaintenanceService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }
}

